import pymysql

def perform_login(username, password):
    try:
        # 连接到数据库
        db = pymysql.connect(
            host="localhost",
            user="root",
            password="123456",
            database="hdwr_user"
        )

        # 创建游标对象
        cursor = db.cursor()

        # 执行查询语句
        cursor.execute("SELECT * FROM user WHERE username=%s AND password=%s", (username, password))

        # 获取一行数据
        data = cursor.fetchone()

        if data:
            # 用户名和密码匹配
            return True
        else:
            # 用户名和密码不匹配
            return False
    except Exception as e:
        # 发生异常时输出异常信息
        print("发生异常:", e)
        return False
    finally:
        # 关闭数据库连接
        if 'db' in locals():
            db.close()

def insert_data(username, password):
    try:
        db = pymysql.connect(
            host="localhost",
            user="root",
            password="123456",
            database="hdwr_user"
        )
        cursor = db.cursor()
        sql = "INSERT INTO user (username, password) VALUES (%s, %s)"
        val = (username, password)
        cursor.execute(sql, val)
        db.commit()
        return True
    except Exception as e:
        print("发生异常:", e)
        return False
    finally:
        db.close()